#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QMessageBox>
#include <QDebug>
#include "userwidget.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget),
    m_networkAccessManager(Q_NULLPTR)
{
    ui->setupUi(this);

    Global::loginWidget = this;

    // 输入完用户名按回车切换到密码
    connect(ui->m_accountLineEdit, &QLineEdit::returnPressed, this, [&]() {
        ui->m_passwordLineEdit->setFocus();
    });

    // 输入完密码按回车自动登录
    connect(ui->m_passwordLineEdit, &QLineEdit::returnPressed, this, [&]() {
        emit ui->m_loginPushButton->clicked();
    });

    // 用户登录
    connect(ui->m_loginPushButton, &QPushButton::clicked, this, [&]() {
        if (!m_networkAccessManager)
            m_networkAccessManager = new QNetworkAccessManager(this);
        connect(m_networkAccessManager, &QNetworkAccessManager::finished, this, [&](QNetworkReply *reply) {
            m_networkAccessManager->disconnect();
            if (reply->readAll() == QString("true"))
            {
                connect(m_networkAccessManager, &QNetworkAccessManager::finished, this, [&](QNetworkReply *reply) {
                    m_networkAccessManager->disconnect();
                    if (reply->readAll() == QString("true"))
                    {
                        Global::userId = ui->m_accountLineEdit->text();
                        Global::userWidget = new UserWidget();
                        Global::userWidget->show();
                        this->hide();
                    }
                    else
                    {
                        QMessageBox::critical(this, u8"登陆失败", u8"更新在线用户表错误，请重试！");
                    }
                });

                // 调用登录接口（更新在线用户表）
                QNetworkRequest request;
                request.setUrl(QUrl(Global::serverIp + QString("/user/login_user?user_id=%1")
                                    .arg(ui->m_accountLineEdit->text())));
                m_networkAccessManager->get(request);
            }
            else
            {
                QMessageBox::critical(this, u8"登陆失败", u8"用户名或密码错误，请重试！");
            }
        });

        // 调用检查接口，检查用户名和密码是否正确
        QNetworkRequest request;
        request.setUrl(QUrl(Global::serverIp + QString("/user/check_login?user_id=%1&password=%2")
                        .arg(ui->m_accountLineEdit->text(), ui->m_passwordLineEdit->text())));
        m_networkAccessManager->get(request);
    });

    // 用户注册
    connect(ui->m_registerPushButton, &QPushButton::clicked, this, [&]() {
        if (!m_networkAccessManager)
            m_networkAccessManager = new QNetworkAccessManager(this);
        connect(m_networkAccessManager, &QNetworkAccessManager::finished, this, [&](QNetworkReply *reply) {
            m_networkAccessManager->disconnect();
            if (reply->readAll() == QString("true"))
            {
               QMessageBox::information(this, u8"注册成功", u8"注册成功，请登录。");
            }
            else
            {
                QMessageBox::critical(this, u8"注册失败", u8"用户名重复，请重试！");
            }
        });
        QNetworkRequest request;
        request.setUrl(QUrl(Global::serverIp + QString("/user/create_user?user_id=%1&password=%2")
                        .arg(ui->m_accountLineEdit->text(), ui->m_passwordLineEdit->text())));
        m_networkAccessManager->get(request);
    });
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

